package com.kgc.springboot.hangduo.controller;

import com.aliyuncs.http.HttpResponse;
import com.github.pagehelper.PageInfo;
import com.kgc.springboot.hangduo.pojo.Client;
import com.kgc.springboot.hangduo.pojo.Feedback;
import com.kgc.springboot.hangduo.service.FeedbackService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @program: hangduo
 * @author: Manxj
 * @create: 2020-12-28 17:21
 */
@Controller
public class FeedbackController {
    @Resource
    FeedbackService feedbackService;

    //进入区

    /**
     * 进入咨询列表
     * @param type 类别
     * @param phone 用户手机号
     * @param content 咨询内容
     * @param pageNum 页码
     * @return
     */
    @RequestMapping("order-list.html")
    public String orderlist(@RequestParam(defaultValue = "5")Integer type,
                            @RequestParam(defaultValue = "%%")String phone,
                            @RequestParam(defaultValue = "%%")String content,
                            @RequestParam(defaultValue = "1")Integer pageNum,
                            Model model){
        if(content.equals("%%")){
            model.addAttribute("content","");
        }else{
            model.addAttribute("content",content);
            content="%"+content+"%";
        }
        PageInfo<Feedback> pageInfo = feedbackService.selectByExample(type, phone, content, pageNum);
        System.out.println(type+"\t"+pageNum+"\t"+phone+"\t"+content);
        System.out.println(phone.equals("%%"));
        if(phone.equals("%%")){
            phone="";
        }
        System.out.println(phone);
        model.addAttribute("type",type);
        model.addAttribute("phone",phone);

        model.addAttribute("pageInfo",pageInfo);
        return "order-list";
    }
//    @RequestMapping("order-list")
//    public String order(@RequestParam(defaultValue = "0")Integer type,
//                            @RequestParam(defaultValue = "%%")String phone,
//                            @RequestParam(defaultValue = "%%")String content,
//                            @RequestParam(defaultValue = "1")Integer pageNum,
//                            Model model){
//        PageInfo<Feedback> pageInfo = feedbackService.selectByExample(type, phone, content, pageNum);
//        model.addAttribute("pageInfo",pageInfo);
//        return "order-list";
//    }



    //修改区

    /**
     * 显示单个资讯页面
     * @return
     */
    @RequestMapping("order-view.html")
    public String orderview(int id){
        System.out.println(id);
        return "order-add";
    }
    @ResponseBody
    @RequestMapping("repostfeed.do")
    public int repostfeed(Integer id){
        System.out.println(id);
        Feedback feedback = new Feedback();
        feedback.setId(id);
        feedback.setResult(1);
        Integer integer = feedbackService.updateById(feedback);
        return integer;
    }









    //添加区
    /**
     * 添加资讯列表页面
     * @return
     */
    @RequestMapping("order-add.html")
    public String orderadd(){
        return "order-add";
    }









    //用户区
    //第一次进去
    @RequestMapping("/hudong.html")
    public String hudong(Model model){
        List<Feedback> list = feedbackService.selectOne();
        model.addAttribute("list",list);
        model.addAttribute("msg","咨询与投诉");
        return "/user/hudong";
    }
    @RequestMapping("/myfeed.html")
    public String hudong(Model model , HttpSession session){
        Client client = (Client) session.getAttribute("client");
        if(client==null)return "redirect:/touser-login";
        List<Feedback> list = feedbackService.selectMy(client.getId());
        model.addAttribute("list",list);
        model.addAttribute("msg","我的咨询");
        return "/user/hudong";
    }



    @RequestMapping("/hudongAdd")
    public String hudongAdd(HttpSession session){
        return "/user/hudongAdd";
    }


    @RequestMapping("/addcontent")
    public String sdd(Feedback feedback,HttpSession session){
        Client client = (Client) session.getAttribute("client");
        if(client==null)return "redirect:/touser-login";
        feedback.setCreateTime(new Date());
        feedback.setUid(client.getId());
        feedbackService.insert(feedback);
        System.out.println(feedback.getContent());
        return "redirect:/myfeed.html";
    }
}
